package edu.jptest.serviceImp;

import java.util.List;

import edu.jptest.bean.Answer;
import edu.jptest.bean.Question;
import edu.jptest.bean.QuestionContent;
import edu.jptest.bean.QuestionSubject;
import edu.jptest.dao.AnswerDAO;
import edu.jptest.dao.QuestionContentDAO;
import edu.jptest.dao.QuestionDAO;
import edu.jptest.dao.QuestionSubjectDAO;
import edu.jptest.service.IAnswerService;
import edu.jptest.service.IQuestionContentService;
import edu.jptest.service.IQuestionService;
import edu.jptest.service.IQuestionSubjectService;

public class QuestionService implements IQuestionService {

	// 插入问题
	public boolean insertQuestion(Question question,
			QuestionSubject questionSubject, QuestionContent questionContent,
			Answer answer) {
		QuestionDAO questionDao = new QuestionDAO();

		IQuestionSubjectService iQuestionSubjectService = new QuestionSubjectService();
		iQuestionSubjectService.insertQuestionSubject(questionSubject);
		int subjectId = iQuestionSubjectService
				.getQuestionSubjectIdByQuestionSubject(questionSubject
						.getQuestionBookNum(), questionSubject
						.getQuestionSubjectContent());
		question.setQuestionSubjectId(subjectId); // 赋值试题题目ID

		IQuestionContentService iContentService = new QuestionContentService();
		iContentService.insertQuestionContent(questionContent);
		int contentId = iContentService
				.getQuestionContentIdByContent(questionContent
						.getQuestionContent());
		question.setQuestionContentId(contentId); // 赋值试题大题干ID

		IAnswerService iAnswerService = new AnswerService();
		iAnswerService.addAnswer(answer);
		int answerId = iAnswerService.getAnswerIdByAnswer(answer);
		question.setAnswerId(answerId); // 赋值答案ID

		if (question.getDoTimes() == null) {
			question.setDoTimes(0);
		}
		if (question.getRightCounts() == null) {
			question.setRightCounts(0);
		}

		return questionDao.insertQuestion(question);
	}

	// 修改问题
	public boolean updateQuestion(Question question, Answer answer,
			QuestionContent questionContent, QuestionSubject questionSubject) {
		QuestionDAO questionDao = new QuestionDAO();
		AnswerDAO answerDAO = new AnswerDAO();
		answerDAO.updateAnswer(answer);

		if (questionContent != null) {
			QuestionContentDAO questionContentDAO = new QuestionContentDAO();
			questionContentDAO.updateQuestionContent(questionContent);
		}

		QuestionSubjectDAO questionSubjectDAO = new QuestionSubjectDAO();
		questionSubjectDAO.updateQuestionSubject(questionSubject);

		return questionDao.updateQuestion(question);
	}

	// 删除问题
	public boolean deleteQuestion(int questionId) {
		QuestionDAO questionDao = new QuestionDAO();
		boolean result = questionDao.deleteQuestion(questionId);

		Question question = questionDao.getQuestionByQuestionId(questionId);
		AnswerDAO answerDAO = new AnswerDAO();
		answerDAO.deleteAnswer(question.getAnswerId());
		// 未删除试卷中试题。。。。
		return result;
	}

	// 通过问题id，查询问题对象
	public Question getQuestionByQuestionId(int question_id) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionByQuestionId(question_id);
	}

	// 通过来源id，查找该来源下的所有试题id
	public List<Integer> getQuestionsIdByOriginId(int origin_id) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByOriginId(origin_id);
	}

	// 通过试题来源、试题题目，获得相关条件下的试题集合
	public List<Integer> getQuestionsIdByOriginIdAndSubject(int origin_id,
			int questionSubject_id) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByOriginIdAndSubject(origin_id,
				questionSubject_id);
	}

	// 通过试题来源、已组试题题目，获得相关条件下的试题集合
	public List<Integer> getQuestionsIdByOriginIdAndSubjectInPaper(
			int origin_id, int questionSubject_id) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByOriginIdAndSubjectInPaper(origin_id,
				questionSubject_id);
	}

	// 通过题型id，查找相关试题集合ͨ������ID�õ���������ID
	public List<Integer> getQuesByType(int typeId) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByType(typeId);
	}

	// 通过题型id、考点id，查找相关试题集合ͨ������id������id�������������ID����
	public List<Integer> getQuestionsIdByTypeAndTestPoint(int questionTypeId,
			int testPointId) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByTypeAndTestPoint(questionTypeId,
				testPointId);
	}

	// 根据作者得到试题ͨ������˵õ���������
	public List<Integer> getQuesByAuthor(int authorId) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsByAuthor(authorId);
	}

	// 通过录入时间，查找某段时间内系统录入的试题
	public List<Integer> getQuestionsIdByTime(java.sql.Date startDate,
			java.sql.Date endDate) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdByTime(startDate, endDate);
	}

	// 通过题目id， 查找此题目下的试题id
	public List<Integer> getQuestionsIdBySubjectId(int subjectId) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getQuestionsIdBySubjectId(subjectId);
	}

	// 通过已组试卷中题目id， 查找试卷中此题目下的试题id
	public List<Integer> getQuestionsIdByExaminPaperSubjectId(
			int examinePaperSubjectId) {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao
				.getQuestionsIdByExaminPaperSubjectId(examinePaperSubjectId);
	}

	// 获得所有问题id
	public List<Integer> getAllQuestionsId() {
		QuestionDAO questionDao = new QuestionDAO();
		return questionDao.getAllQuestionsId();
	}
}
